package com.googlecode.ozonbooksfinder.cairngormfinder.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.business.BookDetailsDelegate;
	import com.googlecode.ozonbooksfinder.cairngormfinder.event.GetBookDetailsEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.event.GetBookInfoEvent;
	import com.googlecode.ozonbooksfinder.cairngormfinder.model.CairngormFinderModelLocator;
	import com.googlecode.ozonbooksfinder.cairngormfinder.utils.BuildStronglyTypedBookVO;
	import com.googlecode.ozonbooksfinder.domain.Book;
	
	import mx.rpc.IResponder;
	
	public class GetBookDetailsCommand extends SequenceCommand implements ICommand, IResponder
	{
		private var model:CairngormFinderModelLocator = CairngormFinderModelLocator.getInstance();
		
		private var bookToken:Book;
		
		override public function execute(event:CairngormEvent):void
	   	{
	   		var delegate:BookDetailsDelegate = new BookDetailsDelegate(this);
	   		bookToken = GetBookDetailsEvent(event).book;
	   		model.currentBook = bookToken;
	   		
	   		if(!bookToken.isExtendedDetailsAdded)
	   		{
		   		nextEvent = new GetBookInfoEvent(bookToken);
		   		delegate.getItemDetails(bookToken.id);
	   		}
	   	}
	   	public function result(event:Object):void
		{
			var book:Book = BuildStronglyTypedBookVO.build(event.result.Tables.ItemDetail.Rows[0]);
			bookToken.addExtendedDetails(book);
			
			executeNextCommand();
		}
	   	public function fault(event:Object):void
		{
			trace( "GetBookDetails Error:\n" + event.fault.faultDetail );
		}
	}
}